package fr.red5.managers ;

/*
 * RED5 Open Source Flash Server - http://www.osflash.org/red5
 * 
 * Copyright (c) 2006-2007 by respective authors (see below). All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or modify it under the 
 * terms of the GNU Lesser General Public License as published by the Free Software 
 * Foundation; either version 2.1 of the License, or (at your option) any later 
 * version. 
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along 
 * with this library; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.red5.server.api.IClient;
import org.red5.server.api.IScope;
import org.red5.server.api.ScopeUtils;
import org.red5.server.api.so.ISharedObject;
import org.red5.server.api.so.ISharedObjectService;

import fr.red5.vo.ClientVO;

/**
 * Class that keeps a list of client names in a SharedObject.
 * 
 * @author 

 */
public class ClientManager {

	private static final Log log = LogFactory.getLog(ClientManager.class);
	
	/** Stores the name of the SharedObject to use. */
	private static String NAME = "user_manager";
	private IScope scope;

	/**
	 * Create a new instance of the client manager.
	 * 
	 * @param name
	 * 			name of the shared object to use
	 * @param persistent
	 * 			should the shared object be persistent
	 */
	public ClientManager(IScope scope) {
		this.scope = scope;
		ISharedObjectService service = (ISharedObjectService) ScopeUtils.getScopeService(scope,ISharedObjectService.class,false);
		service.createSharedObject(scope, NAME, false);		
	}

	/**
	 * Return the shared object to use for the given scope.
	 * 
	 * @param scope
	 * 			the scope to return the shared object for
	 * @return the shared object to use
	 */
	private ISharedObject getSharedObject() {
		ISharedObjectService service = (ISharedObjectService) ScopeUtils
				.getScopeService(scope,
						ISharedObjectService.class,
						false);
		return service.getSharedObject(scope, NAME, false);
	}

	/**
	 * A new client connected. This adds the username to
	 * the shared object of the passed scope. 
	 * 
	 */
	public void addClient(IClient client) {
		ISharedObject so = getSharedObject();
		String username = (String)client.getAttribute("username");
		String uid = client.getId();
		ClientVO clientVO = new ClientVO(uid,username);		
		so.setAttribute(uid,clientVO);
		log.debug("addClient:"+username);
	}

	/**
	 * A client disconnected. This removes the username from
	 * the shared object of the passed scope.
	 * 
	 */
	public String removeClient(IClient client) {
		ISharedObject so = getSharedObject();
		String uid = client.getId();
		if (!so.hasAttribute(uid)) {
			// SharedObject is empty. This happes when the last client
			// disconnects.
			return null;
		}

		String username = so.getStringAttribute(uid);
		so.removeAttribute(uid);
		log.debug("removeClient:"+username);
		return username;
	}

}
